/**
 * CS307 Team 11 Spring 2011
 * @author Kevin Barlow
 * @author David Barnes
 * @author Max Cohen
 * @author Yoongu Kang
 * @author Matt Lathrop
 * @author Thomas Maley
 * 
 * The two main types of Event are Free-time and NormalEvent. 
 * A NormalEvent is the type of Event created by an admin.  
 * Reminders can be associated with this event.  
 * A Free-time Event is the type of Event created by Volunteers.  
 * Free-time Events are used by the Calendar when determining which 
 * Volunteers may be available for a certain NormalEvent for which they are not currently signed up.
 * 
 * 
 */

package com.cs307.redcross.client;

import java.io.Serializable;
import java.util.Date;
import java.util.Vector;

import com.google.gwt.i18n.client.DateTimeFormat;

public class Event implements Serializable {
	
	private static final long serialVersionUID = -5748471833052505973L;
	
	protected Date startTime;	 //stores the start time for the event
	protected Date endTime;		 //stores the end time for the event
	protected int id;
	protected String name;			//stores the name of the event
	protected String recurrence;	//stores the recurrence string
	/*
	 * thought for recurrence
	 * [D|W|M|Y]:[M|T|W|H|F] | [DOM [0-31] | DOW [0-31] [M|T|W|H|F]] | [Date]:[0-9]*
	 * 
	 * First parameter will be daily, weekly, monthly yearly
	 * 
	 * Second parameter:
	 * 
	 * Daily: The days on which the event needs to occur Monday, Tuesday, Wednesday, Thursday, Friday
	 *	 
	 * Weekly: Same as daily except the Third parameter will be different
	 * 		   
	 * Monthly:
	 * 			DOM: followed by the day at which the occurrence should happen
	 * 			DOW: followed by the number it will occur then the day it will occur (example: 2nd Saturday )
	 * 
	 * Yearly: the date it should occur on
	 * 
	 * Third parameter: number of occurrences
	 */
	
	private Vector<Person> people;	//stores all the people that are currently working on the event
	private Vector<Group> groups;	//stores all the groups for the event
	private String description;
	
	protected int nextEventID; //use -1 to represent no next event
	
	public int getNextEventID() {
		return nextEventID;
	}

	public void setNextEventID(int nextEventID) {
		this.nextEventID = nextEventID;
	}

	public Date getStartTime() {
		return startTime;
	}
	
	// get date in string properly formatted for the client
	public String getStartTimeString() {
		return DateTimeFormat.getFormat("MM/dd/yy").format(startTime);
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}
	
	// get date in string properly formatted for the client
	public String getEndTimeString() {
		return DateTimeFormat.getFormat("MM/dd/yy").format(endTime);
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getRecurrence() {
		return recurrence;
	}

	public void setRecurrence(String recurrence) {
		this.recurrence = recurrence;
	}

	public Vector<Person> getPeople() {
		return people;
	}

	public void setPeople(Vector<Person> people) {
		this.people = people;
	}

	public Vector<Group> getGroups() {
		return groups;
	}

	public void setGroups(Vector<Group> groups) {
		this.groups = groups;
	}

	public int getId() {
		return id;
	}
	
	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return description;
	}

	public Vector<Person> getFreePeople() {
		Vector<Person> tpeople = new Vector<Person>();
		Vector<Person> pfree = new Vector<Person>();
		
		//adds all the people in all the groups to a temp array
		if ( groups != null ){
			for( Group g : groups ){
				for( Person p : g.getPersonList() ){
					if ( tpeople.indexOf( p ) < 0){
						tpeople.add( p );
					}
				}
			}
		}
		
		//adds all the people to the list
		for ( Person p : people ){
			
			if ( tpeople.indexOf( p ) < 0 ){
				tpeople.add( p );
			}
		}
		//goes through all the people and checks if they are free at that time
		//if they are free add them to pfree Vector
		//if not continue

		for ( Person p : tpeople){
			for( FreeTimeEvent fte : p.getFreeTimeEvents() ){
				
				System.out.println( fte.name + ":");
				//System.out.println( fte );
				if ( fte.getEndTime().before( startTime ) || fte.getStartTime().after( endTime ) ) {
					System.out.println( "passed ");
					if ( pfree.indexOf( p ) < 0)
						pfree.add( p );
				}else {
					//may want to break here
					pfree.remove( p );
					System.out.println( "failed");
				}
				System.out.println();
			}
		}
		
		return pfree;
	}
	
	public Event(String n, String d, Date s, Date e, String r, int next, Vector<Group> g, Vector<Person> p)
	{
		this.id = -1;
		this.name = n;
		this.description = d;
		this.startTime = s;
		this.endTime = e;
		this.recurrence = r;
		this.groups = g;
		this.people = p;
		this.nextEventID = next;
	}
	
	public Event(Event event)
	{
		this.id = event.getID();
		
		if (name != null)
			this.name = event.getName();
		else {
			this.name = "";
		}
		
		if (this.description != null)
			this.description = event.getDescription();
		else
			this.description = "";
	
		if (event.getStartTime() != null)
			this.startTime = (Date) event.getStartTime().clone();
		else
			this.startTime = new Date();
		
		if (event.getEndTime() != null)
			this.endTime = (Date) event.getEndTime().clone();
		else
			this.endTime = new Date();
		
		if (this.recurrence != null)
			this.recurrence = event.getRecurrence();
		else
			this.recurrence = "";
		
		if (event.getGroups() != null)
			this.groups = new Vector<Group>(event.getGroups());
		else
			this.groups = new Vector<Group>();
		if (event.getPeople() != null)
			this.people = new Vector<Person>(event.getPeople());
		else 
			this.people = new Vector<Person>();
		
		this.nextEventID = event.getNextEventID();	
	}
	
	public Event(EventTemplate e)
	{
		this.id = -1;
		this.name = e.getName();
		this.description = e.getDescription();
		this.startTime = e.getStartTime();
		this.endTime = e.getEndTime();
		this.recurrence = e.getRecurrence();
		this.groups = e.getGroups();
		this.people = e.getPeople();
		this.nextEventID = -1;
	}
	
	public Vector<Event> getAllFutureEvents(){
		Vector<Event> events = new Vector<Event>();
		//TODO: populate events list based on recurrence and returns all the events
		/**
		/ for each new event needs to fill all values needed
		/ needs call getNextID to get the next events ID
		*/
		
		
		
		return events;
	}
	// updateFutureEvents( Event e ) updates each event 
	public Vector<Event> updateFutureEvent( Event e ){
		/**
		 * updates all events after e and returns the new Vector of them
		 * needs to use getEvent( int id ) to get each one
		 * use get NextEventID for this
		 */
		Vector<Event> events = new Vector<Event>();
	
		return events;
	}
	public Event(){
		this("", "", null, null, "", -1, null, null);
	}
	
	@Override
	public String toString() {
		return "Event [startTime=" + startTime + ", endTime=" + endTime
				+ ", id=" + id + ", name=" + name + ", nextEventID="
				+ nextEventID + ", recurrence=" + recurrence +  "]";
	}

	public boolean equals( Event e ){
		
		return this.getId() == e.getId();
	}
	public boolean createReminders()
	{
		return true;
	}
	
	public int getID()
	{
		return this.id;
	}
	public void setId(int id) {
		this.id = id;
	}
	/**
	 * Only used for debug purposes
	 * @param args
	 */
	@SuppressWarnings("deprecation")
	public static void main( String[] args ){
		System.out.println( "Event" );
		Date start, end;
		FreeTimeEvent fte;
		Vector<FreeTimeEvent> free = new Vector<FreeTimeEvent>();
		Vector<Person> p = new Vector<Person>();
		Volunteer v;
		
		//creates a new free time event for a person
		
		//should pass
		start = new Date( 2011 - 1900, 11, 10, 11, 00);	//12/10/2011 11:00
		end = new Date( 2011 -1900, 11, 10, 11, 30);		//12/10/2011 12:30
		fte = new FreeTimeEvent( 1, "event1", start, end, "rec", 3 ); //free from 12:20 to 12:30 on 12/10/2011
		free.add( fte );
		
		//should pass
		start = new Date( 2011 - 1900, 11, 12, 13, 20);		//12/12/2011 1:00
		end   = new Date( 2011 -1900, 11, 12, 14, 00);		//12/12/2011 2:00
		fte = new FreeTimeEvent( 2, "event2", new Date( 2011 - 1900, 11, 12, 13, 20), end, "rec", 4);
		free.add( fte );
		
		v = new Volunteer( "first", "last", "email", "carrier", "cell" ,"sms", "username", "password", free);
		
		p.add( v );
		
		free = new Vector<FreeTimeEvent>();
		//should fail
		start = new Date( 2011 - 1900, 11, 12, 11, 00);		//12/12/2011 11:00
		end   = new Date( 2011 -1900, 11, 12, 12, 00);		//12/12/2011 12:00
		fte = new FreeTimeEvent( 2, "event3",start, end, "rec", 4);
		free.add( fte );
		
		//should pass
		start = new Date( 2011 - 1900, 11, 12, 10, 00);		//12/12/2011 10:00
		end   = new Date( 2011 -1900, 11, 12, 11, 00);		//12/12/2011 11:00
		fte = new FreeTimeEvent( 2, "event4", start, end, "rec", 4);
		free.add( fte );
		
		//should fail
		start = new Date( 2011 - 1900, 11, 12, 11, 45);		//12/12/2011 11:45
		end   = new Date( 2011 -1900, 11, 12, 12, 15);		//12/12/2011 12:15
		fte = new FreeTimeEvent( 2, "event5", start, end, "rec", 4);
		free.add( fte );
		
		v = new Volunteer( "first1", "last1", "email", "carrier", "cell" , "sms", "username", "password", free );
		p.add( v );
		
		//												12/12/2011 11:30	-		12/12/2011 12:30
		Event e = new Event( "happy", "a happy event", new Date(2011-1900, 11, 12, 11, 30), new Date(2011-1900, 11, 12, 12, 30), "rec", 1, new Vector<Group>(), p );
		
		p = e.getFreePeople();
		
		for ( Person pe : p ){
			System.out.println( pe.getFirstName() + " " + pe.getLastName() );
		}
	}
	
}
